classdef DataSourceAdapter < handle
    %UNTITLED16 Summary of this class goes here
    %   Detailed explanation goes here
    
 
    
    methods(Static)
        function returnData = getDividendData(timeList,tickerList,distType)
            returnData = zeros(length(tickerList),length(timeList));
            stockTickerList = tickerList(tickerList < 10000);
            if isempty(stockTickerList)
                return;
            end
            
            len=length(stockTickerList);
            tickStr=cell(1,2*len-1);
            tickStr(:,:)={','};
            
            for i = 1:len
                tickStr{2*i-1} = num2str(stockTickerList(i));
            end
            tickStr=cell2mat(tickStr);
            
            startDate = datestr(timeList(1,1),'yyyy-mm-dd');
            endDate = datestr(timeList(1,end),'yyyy-mm-dd');
            sql = ['exec QueryDividendByOrgID ''',tickStr,''',''',startDate,''',''',endDate,''',''',distType,''''];
            tcurs = exec(getConn('QuantFundamentalData'), sql);
            tcurs = fetch(tcurs, 100000);
            if strcmp(tcurs.Data{1,1},'No Data')
                return;
            end
            
            data = tcurs.data;
            [rtf,rlof] = ismember(cell2mat(data(:,1)), tickerList);
            [ctf,clof] = ismember(datenum(cell2mat(data(:,5))), timeList);
            
            logicIdx = logical(rtf)&logical(ctf);
            clof = clof(logicIdx);
            rlof = rlof(logicIdx);
            validData = cell2mat(data(:,4));
            validData = validData(logicIdx,:);
            returnData(sub2ind(size(returnData), rlof, clof))  =  validData;
        end
    end
    
end

